package main

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

// User 结构体声明
type User struct {
	UserId    int64  `gorm:"primaryKey;autoIncrement"`
	UserName  string `gorm:"not null;type:varchar(32)"`
	UserPwd   string `gorm:"not null;type:varchar(128)"`
	UserPhone string `gorm:"unique;type:varchar(32)"`
}

func (*User) TableName() string {
	return "t_user"
}

func main() {
	// 连接数据库
	var dsn = "root:QWEzxc123456@tcp(127.0.0.1:3306)/gorm_test?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Println("数据库连接失败!", err)
	}
	err = db.AutoMigrate(&User{})
	if err != nil {
		fmt.Println("数据库表迁移失败!", err)
	}
}
